package com.acgist.snail.java11;

import cn.hutool.core.util.ByteUtil;

public class ArraysJava11 {
    public static int compareUnsigned(byte[] a, byte[] b) {
        if (a == b)
            return 0;
        if (a == null || b == null)
            return a == null ? -1 : 1;

        int i = ArraysSupportJava11.mismatch(a, b,
                Math.min(a.length, b.length));
        if (i >= 0) {
            return ByteUtil.byteToUnsignedInt(a[i]) - ByteUtil.byteToUnsignedInt(b[i]);
        }

        return a.length - b.length;
    }

    public static int mismatch(byte[] a, byte[] b) {
        int length = Math.min(a.length, b.length); // Check null array refs
        if (a == b)
            return -1;

        int i = ArraysSupportJava11.mismatch(a, b, length);
        return (i < 0 && a.length != b.length) ? length : i;
    }

}
